layui.use(["jquery","upload"],function () {
    var $ = layui.jquery;
    var upload = layui.upload;
    var allowClick = false;
    var imageName;
    var carOutImageUploadBtn = upload.render({
        elem: '#carOutImageUploadBtn' //绑定元素
        ,url: '/uploadImg/' //上传接口
        ,done: function(res, index, upload){
            //上传完毕回调
            console.info(res);
            console.info(upload);
            console.info(index);
            console.info("success");
            if(res.carNo[1] === "-1"){
                layer.alert("识别失败，请手动输入车牌号和车辆类型")
            }
            $("#carId").val(res.carNo[1]);
            $("#carType").val(res.carNo[2]);
            $("#carOutShow").attr("src","..//img//carImg//"+res.carNo[0]);
            imageName=res.carNo[0];
            allowClick = true;
        }
        ,error: function(index, upload){
            layer.alert("服务器出错")
        }
        ,accept: 'images'
        ,acceptMime: 'image/*'
    });
    $("#carOutBut").on("click",function () {
        var carId = $("#carId").val();
        var carType = $("#carType").val();


        if (allowClick){
            if(carId.length<5||(carType!=="大型车")&&(carType!=="小型车")){
                layer.alert("请输入车牌号和车类型");
            }else{
                $.ajaxSettings.async = false;
                let recordId = 0;
                $.ajax({
                    url: "/getRecordId",
                    data: {
                        carId:carId
                    },
                    success: function (res) {
                        console.info(res);
                        recordId = res;
                    }
                });
                let price = 0;
                $.ajax({
                    url: "/manage/Calculate",
                    data: {
                        id: recordId
                    },
                    success: function (res) {
                        console.info(res);
                        price = res;
                    }
                });
                // $.ajax({
                //     type:"post",
                //     url:"/payment/pay",
                //     data:{
                //         id:recordId,
                //         price:price,
                //     },
                //     success: function (res) {
                //         layer.msg("入库成功");
                //     },
                //     error: function (res) {
                //
                //     }
                // });
                layer.confirm(`共计${price}元`, {
                    title: "选择支付方式",
                    btn: ['现金支付','支付宝支付'] //按钮
                },function(){
                    $.ajax({
                        type:"post",
                        url:"/carOut",
                        data:{
                            carId:carId,
                            carType:carType,
                            image:imageName
                        },
                        success: function (res) {
                            $("#carOutShow").attr("src","..//img//move.gif");
                            layer.msg("支付成功");
                        },
                        error: function (res) {

                        }
                    })
                },function () {
                    window.open(`../payment/pay?price=${price}&id=${recordId}`);
                    //window.location.href=`../payment/pay?price=${price}&id=${recordId}`;
                    $.ajax({
                        type:"post",
                        url:"/carOut",
                        data:{
                            carId:carId,
                            carType:carType,
                            image:imageName
                        },
                        success: function (res) {
                            $("#carOutShow").attr("src","..//img//move.gif");
                            layer.msg("支付成功");
                        },
                        error: function (res) {

                        }
                    })
                });

            }


        }else{
            layer.alert("当前状态不允许点击入库")
        }

    })
});